package com.liujit.common.domain.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.liujit.common.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author: liujun
 * @date: 2021/6/15 10:32 下午
 * @description: 接口默认返回结果
 */
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@JsonPropertyOrder({ "code", "msg", "data" })
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ApiModel(value = "Result", description = "接口默认返回结果")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 7195922113580054746L;

    @ApiModelProperty(value = "接口返回code")
    private Integer code;

    @ApiModelProperty(value = "接口返回信息")
    private String msg;

    @ApiModelProperty(value = "接口返回数据")
    private T data;

    /**
     * 成功
     *
     * @return
     * @author: liujun
     * @date: 2021/6/15 10:43 下午
     */
    public static <T> Result<T> success() {
        ResultCode resultCode = ResultCode.SUCCESS;
        return result(resultCode.code(), resultCode.msg());
    }

    /**
     * 成功
     *
     * @param data
     * @return
     * @author: liujun
     * @date: 2021/6/15 10:43 下午
     */
    public static <T> Result<T> success(T data) {
        return result(ResultCode.SUCCESS, data);
    }

    /**
     * 失败
     *
     * @param resultCode
     * @return
     * @author: liujun
     * @date: 2021/6/15 10:43 下午
     */
    public static <T> Result<T> failure(ResultCode resultCode) {
        return result(resultCode.code(), resultCode.msg());
    }

    /**
     * 失败
     *
     * @param msg
     * @return
     * @author: liujun
     * @date: 2021/6/15 10:45 下午
     */
    public static <T> Result<T> failure(String msg) {
        return result(ResultCode.FAILURE.code(), msg);
    }

    /**
     * 失败
     *
     * @param resultCode
     * @param data
     * @return
     * @author: liujun
     * @date: 2021/6/15 10:45 下午
     */
    public static <T> Result<T> failure(ResultCode resultCode, T data) {
        return result(resultCode, data);
    }

    /**
     * 失败
     *
     * @param code
     * @param msg
     * @return
     * @author: liujun
     * @date: 2021/6/18 7:16 下午
     */
    public static <T> Result<T> failure(Integer code, String msg) {
        return result(code, msg);
    }

    private static <T> Result<T> result(ResultCode resultCode, T data) {
        return result(resultCode.code(), resultCode.msg(), data);
    }

    private static <T> Result<T> result(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    private static <T> Result<T> result(Integer code, String msg, T data) {
        return new Result<>(code, msg, data);
    }
}
